package me.mononin.factory.data.group;

import android.text.TextUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import me.mononin.factory.data.helper.DbHelper;
import me.mononin.factory.data.helper.GroupHelper;
import me.mononin.factory.data.helper.UserHelper;
import me.mononin.factory.model.card.GroupCard;
import me.mononin.factory.model.card.GroupMemberCard;
import me.mononin.factory.model.db.Group;
import me.mononin.factory.model.db.GroupMember;
import me.mononin.factory.model.db.User;

public class GroupDispatcher implements GroupCenter {
    //单例
    public static GroupDispatcher instance;
    //新建线程池
    private final Executor executor = Executors.newSingleThreadExecutor();

    //单例获取函数
    public static GroupDispatcher instances(){
        if (instance==null){
            synchronized (GroupDispatcher.class){
                if (instance==null){
                    instance = new GroupDispatcher();
                }
            }
        }
        return instance;
    }

    @Override
    public void dispatch(GroupCard... cards) {
        if (cards == null || cards.length == 0)
            return;
        //在线程池中执行
        executor.execute(new GroupCardHandle(cards));
    }

    @Override
    public void dispatch(GroupMemberCard... memberCards) {
        if (memberCards==null||memberCards.length==0){
            return;
        }
        executor.execute(new GroupMemberCardHandle(memberCards));
    }

    /**
     * 群组数据库异步操作分发
     */
    private class GroupCardHandle implements Runnable{
        private final GroupCard[] cards;

        public GroupCardHandle(GroupCard... cards){
            this.cards = cards;
        }
        @Override
        public void run() {
            List<Group> groups = new ArrayList<>();

            for (GroupCard card : cards){
                //判空
                if (card==null){
                    continue;
                }
                User owner = UserHelper.search(card.getOwnerId());
                if (owner!=null){
                    Group group = card.build(owner);
                    groups.add(group);
                }
            }

            if (groups.size()>0){
                DbHelper.save(Group.class,groups.toArray(new Group[0]));
            }
        }
    }

    /**
     * 群成员数据库异步操作
     */
    private class GroupMemberCardHandle implements Runnable{
        private final GroupMemberCard[] cards;

        public GroupMemberCardHandle(GroupMemberCard... cards){
            this.cards = cards;
        }
        @Override
        public void run() {
            List<GroupMember> groupMembers = new ArrayList<>();

            for (GroupMemberCard card : cards){
                //判空
                if (card==null){
                    continue;
                }
                //对应成员信息
                User user = UserHelper.search(card.getUserId());
                //对应群组信息
                Group group = GroupHelper.find(card.getGroupId());
                if (user!=null&&group!=null){
                    //创建群成员信息
                    GroupMember member = card.build(group,user);
                    //添加群成员信息
                    groupMembers.add(member);
                }
                //判断是否含有群成员
                if (groupMembers.size()>0){
                    //保存群成员信息到本地
                    DbHelper.save(GroupMember.class,groupMembers.toArray(new GroupMember[0]));
                }
            }
        }
    }
}
